package com.web.expspringboot;

import com.web.expspringboot.entity.MessageSt;
import com.web.expspringboot.entity.ProductSt;
import com.web.expspringboot.entity.UserSt;
import com.web.expspringboot.mapper.ProductMapper;
import com.web.expspringboot.mapper.UserMapper;
import com.web.expspringboot.service.PurchaseService;
import com.web.expspringboot.service.ProductService;
import com.web.expspringboot.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class SpringbootTestMessageService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductService productService;

    @Autowired
    UserService userService;

    @Autowired
    PurchaseService messageService;

    @Test
    void generateMessages() {
        List<ProductSt> products = productMapper.selectAllProducts(1,10);
        List<UserSt> allUsers = userMapper.getAllUser();
        int userCount = allUsers.size();
        int productCount = products.size();
        System.out.println("----------------------------------------------------------------------");
        System.out.println("userCount:" + userCount);
        System.out.println("productCount:" + productCount);
        System.out.println("----------------------------------------------------------------------");
        for (ProductSt product : products) {

            MessageSt message = new MessageSt();
            message.setProductId(product.getProductId());
            System.out.println("************************************");
            System.out.println("product:" + product.getProductId());
            for (UserSt user : allUsers) {
                if ((Math.random()) > (20f / userCount)) {
                    continue;
                }
                int numMessages = (int) (Math.random() * 4) + 3;
                for (int i = 0; i < numMessages; i++) {
                    if (Math.random() > 0.5) {
                        message.setUserId(product.getOwnerId());
                    } else {
                        message.setUserId(user.getUserId());
                    }
                    message.setText("Test message from user "
                            + user.getUserId() + " to product "
                            + product.getProductId());
                    message.setTime(new java.util.Date());


                }
            }

            System.out.println("************************************");
        }

    }

}
